package com.android.antelopeapp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import com.android.antelopeapp.activity.BaseActivity;

import com.android.antelopeapp.external.paginglistview.PagingListView;
import java.util.List;

/**
 * Created by phk on 15-4-13.
 * 下拉自动加载的listview
 */
public abstract class MyPagingListView<T> extends PagingListView {
    public static String TAG = "MyPagingListView";
    protected int mStartPos;
    protected boolean isEnd = false;

    protected BaseActivity bacty;

    public MyPagingListView(Context context) {
        super(context);
    }

    public MyPagingListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyPagingListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void init(BaseActivity bacty){
        this.bacty = bacty;
        setHasMoreItems(true);
        setPagingableListener(new PagingListView.Pagingable() {
            @Override
            public void onLoadMoreItems() {
                if (!isEnd) {
                    Log.d(TAG, "onLoadMoreItems call updateListView&start=" + mStartPos);
                    updateListView();//往下加载
                } else {
                    Log.d(TAG, "onLoadMoreItems do nothing because the page is the lase");
                }
            }
        });
        initAdapter();
    }

    /**完成一个页面的添加*/
    protected void addPage(){
        mStartPos++;
    }

    /**加载数据到列表，同时停止加载*/
    protected void addItemsToList(boolean notEnd,List<T> clist){
        this.isEnd = !notEnd;
        onFinishLoading(notEnd, clist);
    }

    protected  void addItemsToHeadList(boolean notEnd,List<T>clist){
        this.isEnd=!notEnd;
        onFinishUpdate(notEnd,clist);
    }

    /**需要实现此函数，里面需要设置PagingBaseAdapter类型的adapter,初始化时将会调用此函数*/
    protected abstract void initAdapter();

    /**需要实现此函数，里面需要设置更新机制,每次更新时将会调用此函数*/
    protected abstract void updateListView();
}
